import pandas as pd


def extract_user_features(file_path):
    df = pd.read_csv(file_path)
    df['merchant_id'] = df['merchant_id'].astype(str)
    total_interactions = df.groupby('user_id').size().reset_index(name='用户总交互次数')
    action_counts = df.groupby(['user_id', 'action_type']).size().unstack(fill_value=0)
    action_counts.columns = ['用户点击次数', '用户加购物车次数', '用户购买次数', '用户收藏次数']
    action_counts = action_counts.reset_index()

    total_actions = df.groupby('user_id').size()
    purchase_actions = df[df['action_type'] == 2].groupby('user_id').size()
    purchase_rate = (purchase_actions / total_actions).reset_index(name='用户购买率')
    purchase_rate['用户购买率'] = purchase_rate['用户购买率'].fillna(0)

    result = total_interactions
    for df_to_merge in [action_counts, purchase_rate]:
        result = pd.merge(result, df_to_merge, on='user_id', how='left')
    return result.fillna(0)[['user_id', '用户总交互次数', '用户点击次数',
                             '用户加购物车次数', '用户购买次数', '用户收藏次数', '用户购买率']]


def extract_user_merchant_features(file_path):
    df = pd.read_csv(file_path)
    df['merchant_id'] = df['merchant_id'].astype(str)
    df['time_stamp'] = pd.to_datetime(df['time_stamp'], format='%m%d').dt.strftime('%m-%d')

    user_merchant = df.groupby(['user_id', 'merchant_id']).size().reset_index(name='用户商家交互次数')
    action_counts = df.groupby(['user_id', 'merchant_id', 'action_type']).size().unstack(fill_value=0)
    action_counts.columns = ['用户商家点击次数', '用户商家加购物车次数', '用户商家购买次数', '用户商家收藏次数']
    action_counts = action_counts.reset_index()

    purchase_data = df[df['action_type'] == 2].sort_values(['user_id', 'merchant_id', 'time_stamp'])
    first_purchase = purchase_data.groupby(['user_id', 'merchant_id'])['time_stamp'].first().reset_index(
        name='首次购买时间')
    last_purchase = purchase_data.groupby(['user_id', 'merchant_id'])['time_stamp'].last().reset_index(
        name='最近购买时间')
    purchase_time = pd.merge(first_purchase, last_purchase, on=['user_id', 'merchant_id'])
    purchase_time['购买时长(天)'] = (pd.to_datetime(purchase_time['最近购买时间'], format='%m-%d') -
                                     pd.to_datetime(purchase_time['首次购买时间'], format='%m-%d')).dt.days

    total_actions = df.groupby(['user_id', 'merchant_id']).size().reset_index(name='用户商家总交互次数')
    purchase_actions = df[df['action_type'] == 2].groupby(['user_id', 'merchant_id']).size().reset_index(
        name='用户商家购买次数')
    purchase_rate = pd.merge(total_actions, purchase_actions, on=['user_id', 'merchant_id'], how='left')
    purchase_rate['用户商家购买率'] = purchase_rate['用户商家购买次数'] / purchase_rate['用户商家总交互次数']
    purchase_rate['用户商家购买率'] = purchase_rate['用户商家购买率'].fillna(0)

    result = user_merchant
    for df_to_merge in [action_counts, purchase_time[['user_id', 'merchant_id', '购买时长(天)']],
                        purchase_rate[['user_id', 'merchant_id', '用户商家购买率']]]:
        result = pd.merge(result, df_to_merge, on=['user_id', 'merchant_id'], how='left')
    return result.fillna(0)[['user_id', 'merchant_id', '用户商家交互次数', '用户商家点击次数',
                             '用户商家加购物车次数', '用户商家购买次数', '用户商家收藏次数',
                             '购买时长(天)', '用户商家购买率']]


def extract_merchant_features(df):
    df['merchant_id'] = df['merchant_id'].astype(str)
    merchant_interaction = df.groupby('merchant_id').size().reset_index(name='商家总交互次数')
    merchant_item = df.groupby('merchant_id')['item_id'].nunique().reset_index(name='商家商品数量')
    merchant_cat = df.groupby('merchant_id')['cat_id'].nunique().reset_index(name='商家品类数量')
    merchant_brand = df.groupby('merchant_id')['brand_id'].nunique().reset_index(name='商家品牌数量')
    merchant_user = df.groupby('merchant_id')['user_id'].nunique().reset_index(name='商家用户数量')

    click = df[df['action_type'] == 0].groupby('merchant_id').size().reset_index(name='商家点击次数')
    cart = df[df['action_type'] == 1].groupby('merchant_id').size().reset_index(name='商家加购物车次数')
    buy = df[df['action_type'] == 2].groupby('merchant_id').size().reset_index(name='商家购买次数')
    collect = df[df['action_type'] == 3].groupby('merchant_id').size().reset_index(name='商家收藏次数')

    purchase_rate = pd.merge(merchant_interaction, buy, on='merchant_id', how='left')
    purchase_rate['商家购买次数'] = purchase_rate['商家购买次数'].fillna(0)
    purchase_rate['商家被购买率'] = purchase_rate['商家购买次数'] / purchase_rate['商家总交互次数']
    purchase_rate = purchase_rate[['merchant_id', '商家被购买率']]

    result = df[['merchant_id', 'label']].drop_duplicates()
    for feature in [merchant_interaction, merchant_item, merchant_cat, merchant_brand,
                    merchant_user, click, cart, buy, collect, purchase_rate]:
        result = pd.merge(result, feature, on='merchant_id', how='left')

    result['label'] = result['label'].fillna(0).astype(int)
    return result.fillna(0)[['merchant_id', 'label', '商家总交互次数', '商家商品数量',
                             '商家品类数量', '商家品牌数量', '商家用户数量', '商家点击次数',
                             '商家加购物车次数', '商家购买次数', '商家收藏次数', '商家被购买率']]


def merge_features(file_path):
    df = pd.read_csv(file_path)
    user_feat = extract_user_features(file_path)
    um_feat = extract_user_merchant_features(file_path)
    merchant_feat = extract_merchant_features(df)

    # 第一步：以用户-商家特征为基础合并用户特征
    merged = pd.merge(um_feat, user_feat, on='user_id', how='left')

    # 第二步：合并商家特征
    merged = pd.merge(merged, merchant_feat, on='merchant_id', how='left')

    # 第三步：删除重复特征（通过列名前缀区分维度）
    duplicate_cols = []
    for col in merged.columns:
        if col in ['user_id', 'merchant_id', 'label']:
            continue
        # 检查是否有相似语义的列（示例：通过前缀判断）
        prefix = col.split('_')[0]
        similar_cols = [c for c in merged.columns if c.startswith(prefix) and c != col]
        if not similar_cols:
            continue
        # 此处仅保留带明确维度前缀的列（实际需根据业务逻辑调整）
        if '用户' in col and any('用户' not in c and '商家' not in c for c in similar_cols):
            duplicate_cols.extend([c for c in similar_cols if '用户' not in c and '商家' not in c])
        elif '商家' in col and any('商家' not in c for c in similar_cols):
            duplicate_cols.extend([c for c in similar_cols if '商家' not in c])

    # 去重（保留带维度前缀的列，删除无前缀的重复列）
    unique_cols = [col for col in merged.columns if col not in duplicate_cols]
    merged = merged[unique_cols]

    # 确保包含目标列
    for col in ['user_id', 'merchant_id', 'label']:
        if col not in merged.columns:
            merged[col] = 0

    return merged


if __name__ == "__main__":
    file_path = 'balanced_data.csv'
    merged_data = merge_features(file_path)

    print(f"合并后列数: {len(merged_data.columns)}")
    print(f"合并后行数: {len(merged_data)}")
    print("前5行数据:")
    print(merged_data.head())

    merged_data.to_csv('merged_features_uniq.csv', index=False, encoding='utf-8-sig')